package com.senven.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.api.annotation.LogInfo;
import com.senven.api.commom.ResponsePage;
import com.senven.api.dao.LogMapper;
import com.senven.api.pojo.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

/**
 * @author LQP
 */
@RequestMapping("/api/admin")
@Service
public class LogService {

    @Autowired
    private LogMapper logMapper;

    /**
     * 插入一条日志
     * @param log
     * @return
     */
    public int insertLog(Log log){
        return  logMapper.insert(log);
    }

    /**
     * 分页获取所有日志列表
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getLogList")
    @ResponseBody
    public ResponsePage<Log> queryLogList(@RequestParam(value = "current", defaultValue = "1") int current,
                                          @RequestParam(value = "size", defaultValue = "10") int size){
            Page<Log> page = new Page<Log>(current, size);
            IPage<Log> iPage = logMapper.selectPage(page,null);
            ResponsePage<Log> responsePage= new ResponsePage<Log>();
            responsePage.makePage(iPage);
            return responsePage;
    }

    /**
     * 分页通过日志名获取日志
     * @param name
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getLogListByName")
    @ResponseBody
    public ResponsePage<Log> queryLogListByName(String name, int current, int size){
        Page<Log> page = new Page<Log>(current, size);
        QueryWrapper<Log> wrapper = new QueryWrapper<Log>();
        wrapper.eq("logname",name);
        IPage<Log> iPage = logMapper.selectPage(page,wrapper);
        ResponsePage<Log> responsePage= new ResponsePage<Log>();
        responsePage.makePage(iPage);
        return responsePage;
    }

    @LogInfo(operateType = "登陆日志")
    public void insertLogin(){}

}
